#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/2 09:08
# @Author  : zhaohexin
# @Email   : 1483643520@xxx.com
# @File    : model.py
# @Software: PyCharm
# @FileDescription： 角色模型类创建

from DatabaseOperation.BaseModel import BaseModel, BaseNullModel
from sqlalchemy import Column, VARCHAR, Integer, BigInteger


class Roles(BaseModel):
    """
    角色表
    """
    __tablename__ = "sys_role"
    __table_args__ = ({"comment": "角色表"})

    name = Column(VARCHAR(255), nullable=False, comment="名称")
    level = Column(Integer, nullable=False, comment="角色级别")
    description = Column(VARCHAR(255), nullable=True, default="", comment="角色描述")
    data_scope = Column(VARCHAR(255), nullable=True, default="", comment="数据权限")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {
            "dataScope": self.data_scope,
            "id": self.id,
            "level": self.level,
            "name": self.name,
            "createTime": self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            "updateTime": self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            "createBy": self.create_by,
        }


class RoleDepts(BaseNullModel):
    """
    角色部门关联表
    """
    __tablename__ = "sys_roles_depts"
    __table_args__ = ({"comment": "角色部门关联表"})

    role_id = Column(BigInteger, primary_key=True, comment="角色ID")
    dept_id = Column(BigInteger, primary_key=True, comment="部门ID")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class RoleMenus(BaseNullModel):
    """
    角色菜单关联表
    """
    __tablename__ = "sys_roles_menus"
    __table_args__ = ({"comment": "角色菜单关联表"})

    role_id = Column(BigInteger, primary_key=True, comment="角色ID")
    menu_id = Column(BigInteger, primary_key=True, comment="菜单ID")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
